package com.atguigu.tingshu.order.template;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * title:
 * author: zgl
 * date: 2024/3/16
 * description：
 */
@OrderKinds(OrderType.ALBUM_ORDER_TYPE)
public class AlbumConfirm extends OrderConfirm {
    @Autowired
    public AlbumConfirm(RedisTemplate redisTemplate){
        super(redisTemplate);
    }
    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Resource
    private AlbumInfoFeignClient albumInfoFeignClient;

    @Override
    public void confirm(OrderInfoVo orderInfoVo,TradeVo tradeVo) {
            //查询用户信息
            Result<UserInfo> userInfoResult = userInfoFeignClient.findUserById(AuthContextHolder.getUserId());
            Assert.notNull(userInfoResult, "订单确认页：获取用户信息失败！");
            UserInfo userInfo = userInfoResult.getData();
            Assert.notNull(userInfo, "订单确认页：获取用户信息失败！");
            //查询专辑信息
            Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(tradeVo.getItemId());
            Assert.notNull(albumInfoResult, "订单确认页：获取专辑信息失败！");
            AlbumInfo albumInfo = albumInfoResult.getData();
            Assert.notNull(albumInfo, "订单确认页：获取专辑信息失败！");
            //查询整专辑购买表
            Result<Boolean> flagResult = userInfoFeignClient.findPayAlbum(tradeVo.getItemId());
            Assert.notNull(albumInfoResult, "订单确认页：获取专辑购买信息失败！");
            Boolean flag = flagResult.getData();
            if (flag) {
                throw new GuiguException(ResultCodeEnum.REPEAT_BUY_ITEM);
            }
            //获取vip信息 判断打折的比率
            BigDecimal discount = new BigDecimal(-1);
            if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(new Date())) {
                discount = albumInfo.getVipDiscount();
            } else {
                discount = albumInfo.getDiscount();
            }
            discount = discount.compareTo(new BigDecimal(-1)) == 0 ? new BigDecimal(10) : discount;
            //设置订单原始金额
            BigDecimal originalAmount = albumInfo.getPrice();
            orderInfoVo.setOriginalAmount(originalAmount);
            //设置订单总金额
            BigDecimal orderAmount = originalAmount.multiply(discount).divide(new BigDecimal(10));
            orderInfoVo.setOrderAmount(orderAmount);
            //设置订单减免总金额
            BigDecimal derateAmount = originalAmount.subtract(orderAmount);
            orderInfoVo.setDerateAmount(derateAmount);
            //设置订单减免明细列表
            List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks("整专辑购买太爽了！");
            orderDerateVoList.add(orderDerateVo);
            orderInfoVo.setOrderDerateVoList(orderDerateVoList);

            //设置订单明细列表
            List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setItemId(tradeVo.getItemId());
            orderDetailVo.setItemName(albumInfo.getAlbumTitle());
            orderDetailVo.setItemPrice(orderAmount);
            orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
            orderDetailVoList.add(orderDetailVo);
            orderInfoVo.setOrderDetailVoList(orderDetailVoList);
    }
}
